<?php


namespace app\api\controller;


use Firebase\JWT\JWT;
use think\Request;
use app\api\model\CourseVideoModel;

class ApiSdk
{
    /**
     * 腾讯播放器签名
     */
    public function getsign() {

        // 通过主键获取fileid
        $video_id = Request::instance()->post('video_id/d');
        $r = CourseVideoModel::get_video_path($video_id);

        $appId = 1500008102; // 用户 appid
        $fileId = $r->video_path; // 目标 FileId
        $currentTime = time();
        $psignExpire = $currentTime + 3600; // 可任意设置过期时间，示例1h
        $urlTimeExpire = dechex($psignExpire); // 可任意设置过期时间，16进制字符串形式，示例1h
        $key = "gnvyy1OmzOtfNVLd2HD8";

        $payload = array(
            "appId" => $appId,
            "fileId" => $fileId,
            "currentTimeStamp" => $currentTime,
            "expireTimeStamp" => $psignExpire,
            "urlAccessInfo" => array(
                "t" => $urlTimeExpire
            )
        );

        $jwt = JWT::encode($payload, $key, 'HS256');
        return json(['code'=>1, 'fileid'=>$r->video_path, 'appid'=>$appId, 'sign'=>$jwt]);
    }
}